package com.mission.schedule.my160920.picker;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.mission.schedule.my160920.kankan.wheel.widget.DateObject;
import com.mission.schedule.my160920.kankan.wheel.widget.NumericWheelAdapter;
import com.mission.schedule.my160920.kankan.wheel.widget.OnWheelChangedListener;
import com.mission.schedule.my160920.kankan.wheel.widget.OnWheelChangedListener2;
import com.mission.schedule.my160920.kankan.wheel.widget.StringWheelAdapter;
import com.mission.schedule.my160920.kankan.wheel.widget.WheelView;
import com.mission.schedule.my160920.kankan.wheel.widget.WheelView2;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 自定义的日期选择器
 *
 * @author sxzhang
 */
public class DatePicker1 extends LinearLayout {

    private Calendar calendar = Calendar.getInstance(); //������
    private WheelView2 newYears, newMonths, newDays;
    private OnChangeListener onChangeListener; //onChangeListener
    private final int MARGIN_RIGHT = 0;
    boolean isRN = false;
    int day = 1,year=2000,month=1;
    private NumericWheelAdapter numericWheelAdapter;
    private List<Integer> years = new ArrayList<>();
    private List<Integer> months = new ArrayList<>();
    private List<Integer> days = new ArrayList<>();

    //Constructors
    public DatePicker1(Context context) {
        super(context);
        init(context);
    }

    public DatePicker1(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    /**
     * 初始化
     *
     * @param context
     */
    private void init(Context context) {
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH) + 1;
        day = calendar.get(Calendar.DAY_OF_MONTH);
        if (((year % 100 == 0) && (year % 400 == 0)) || ((year % 100 != 0) && (year % 4 == 0))) {
            isRN = true;
        }

        for (int i = 2000; i <= 2100 ; i++){
            years.add(i);
        }

        for (int i = 1; i <= 12 ; i++){
            months.add(i);
        }

        for (int i = 1; i <= 31 ; i++){
            days.add(i);
        }

        newYears = new WheelView2(context);

        LayoutParams newYears_param = new LayoutParams(200, LayoutParams.WRAP_CONTENT);
        newYears_param.setMargins(0, 0, MARGIN_RIGHT, 0);
        newYears.setLayoutParams(newYears_param);
        newYears.setAdapter(new NumericWheelAdapter(2000, 2100));
        newYears.setVisibleItems(5);
        newYears.setCyclic(true);

//        newYears.setCurrentItem(year - 2000);
        newYears.addChangingListener(onYearsChangedListener);
        addView(newYears);

        newMonths = new WheelView2(context);

        LayoutParams newMonths_param = new LayoutParams(200, LayoutParams.WRAP_CONTENT);
        newMonths_param.setMargins(0, 0, MARGIN_RIGHT, 0);
        newMonths.setLayoutParams(newMonths_param);
        newMonths.setAdapter(new NumericWheelAdapter(1, 12));
        newMonths.setVisibleItems(5);
        newMonths.setCyclic(true);

//        newMonths.setCurrentItem(month - 1);
        newMonths.addChangingListener(onMonthsChangedListener);
        addView(newMonths);

        newDays = new WheelView2(context);

        LayoutParams newDays_param = new LayoutParams(200, LayoutParams.WRAP_CONTENT);
        newDays_param.setMargins(0, 0, MARGIN_RIGHT, 0);
        newDays.setLayoutParams(newDays_param);
        newDays.setAdapter(new NumericWheelAdapter(1, 31));
        newDays.setVisibleItems(5);
        newDays.setCyclic(true);

//        newDays.setCurrentItem(day - 1);
        newDays.addChangingListener(onDaysChangedListener);
        addView(newDays);

        setToday();
    }

    public void setToday(){
        newYears.setCurrentItem((year > 2100 || year < 2000) ? 0 : year - 2000);
        newMonths.setCurrentItem(month - 1);
        newDays.setCurrentItem((day > days.size()) ? days.size() - 1 : day - 1);
    }

    /**
     * 滑动改变监听器
     */
    private OnWheelChangedListener2 onDaysChangedListener = new OnWheelChangedListener2() {
        @Override
        public void onChanged(WheelView2 mins, int oldValue, int newValue) {
            change();
        }
    };
    private OnWheelChangedListener2 onMonthsChangedListener = new OnWheelChangedListener2() {
        @Override
        public void onChanged(WheelView2 mins, int oldValue, int newValue) {

            if ((newValue + 1) < 7 && (newValue + 1) != 2) {
                if ((newValue + 1) % 2 == 0) {
                    newDays.setAdapter(new NumericWheelAdapter(1, 30));
                    setDay(30);
                } else {
                    newDays.setAdapter(new NumericWheelAdapter(1, 31));
                    setDay(31);
                }
            } else if ((newValue + 1) == 7 || (newValue + 1) == 8) {
                newDays.setAdapter(new NumericWheelAdapter(1, 31));
                setDay(31);
            } else if ((newValue + 1) >= 9) {
                if ((newValue + 1) % 2 == 0) {
                    newDays.setAdapter(new NumericWheelAdapter(1, 31));
                    setDay(31);
                } else {
                    newDays.setAdapter(new NumericWheelAdapter(1, 30));
                    setDay(30);
                }
            } else if ((newValue + 1) == 2) {
                if (isRN == true) {
                    newDays.setAdapter(new NumericWheelAdapter(1, 29));
                    setDay(29);
                } else if (isRN == false) {
                    newDays.setAdapter(new NumericWheelAdapter(1, 28));
                    setDay(28);
                }
            }
            change();
        }
    };
    private OnWheelChangedListener2 onYearsChangedListener = new OnWheelChangedListener2() {
        @Override
        public void onChanged(WheelView2 mins, int oldValue, int newValue) {
            if (((newValue % 100 == 0) && (newValue % 400 == 0)) || ((newValue % 100 != 0) && (newValue % 4 == 0))) {
                newDays.setAdapter(new NumericWheelAdapter(1, 29));
                setDay(29);
                isRN = true;
            } else {
                setDay(28);
                newDays.setAdapter(new NumericWheelAdapter(1, 28));
                isRN = false;
            }
            change();
        }
    };

    private void setDay(int max){
        days.clear();
        for (int i = 1; i <= max ; i++){
            days.add(i);
        }
        newDays.setCurrentItem(0);
    }

    /**
     * 滑动改变监听器回调的接口
     */
    public interface OnChangeListener {
        void onChange(int year, int month, int day);
    }

    /**
     * 设置滑动改变监听器
     *
     * @param onChangeListener
     */
    public void setOnChangeListener(OnChangeListener onChangeListener) {
        this.onChangeListener = onChangeListener;
    }

    /**
     * 滑动最终调用的方法
     */
    private void change() {
        if (onChangeListener != null) {
            onChangeListener.onChange(
                    years.get(newYears.getCurrentItem()),
                    months.get(newMonths.getCurrentItem()),
                    days.get(newDays.getCurrentItem()));
        }
    }


    /**
     * 根据day_of_week得到汉字星期
     *
     * @return
     */
    public static String getDayOfWeekCN(int day_of_week) {
        String result = null;
        switch (day_of_week) {
            case 1:
                result = "星期日";
                break;
            case 2:
                result = "星期一";
                break;
            case 3:
                result = "星期二";
                break;
            case 4:
                result = "星期三";
                break;
            case 5:
                result = "星期四";
                break;
            case 6:
                result = "星期五";
                break;
            case 7:
                result = "星期六";
                break;
            default:
                break;
        }
        return result;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


}
